﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace FlockTest
{
    public class FlockElement
    {
        // Current Position and Velociy
        private Vector coordinates;
        private Vector velocity;

        // Used to calculate the current direction the element is being pulled towards
        private Vector pull;

        private float attraction = 0;   // This represents the strength of the flocking behavior
        private float repulsion = 0;    // This represents the elements desire to keep its personal space

        private float arBoundary = 0;   // Attration-Repulsion Boundary

        private float maxVelocity = 0;  // Absolute max velocity of element
        private float maxPull = 0;      // Maximum Pull/Acceleration

        private bool isLeader = false;



        // Last Update Time
        public DateTime time = DateTime.Now;

        public FlockElement()
        {
            // Speed and Flocking Behaviour
            attraction = FlockMath.Rbd();
            repulsion = FlockMath.Rbd();
            arBoundary = FlockMath.Rbd();
            maxVelocity = FlockMath.Rbd();
            maxPull     = FlockMath.Rbd();

            // Start positions
            coordinates = new Vector(FlockMath.Rbd() * Global.BOUNDING_X,
                              FlockMath.Rbd() * Global.BOUNDING_Y,
                              FlockMath.Rbd() * Global.BOUNDING_Z);

            velocity = new Vector(FlockMath.Rbd() * maxVelocity / 1.7325F,
                           FlockMath.Rbd() * maxVelocity / 1.7325F,
                           FlockMath.Rbd() * maxVelocity / 1.7325F);

            pull = Vector.Zero;
        }



        public Vector Coordinates
        {
            get { return coordinates; }
            set { coordinates = value; }
        }
        public Vector Velocity
        {
            get { return velocity; }
            set { velocity = value; }
        }

        public Vector Pull
        {
            get { return pull; }
            set { pull = value; }
        }

        public float Attraction     { get { return attraction * Global.ATTRACTION; } }
        public float Repulsion      { get { return repulsion * Global.REPULSION; } }

        public float ArBoundary     { get { return arBoundary * Global.AR_BOUNDARY; } }
        public float MaxVelocity    { get { return maxVelocity * Global.MAX_VELOCITY; } }
        public float MaxPull        { get { return maxPull * Global.MAX_PULL; }}

        public void Tick() // Update Last Time
        {
            time = DateTime.Now;
        }

        public float DeltaTime() // Seconds from last Tick
        {
            return (float)(DateTime.Now - time).TotalSeconds;
        }

        public bool IsLeader
        {
            get { return isLeader; }
            set { isLeader = value; }
        }
    }
}
